﻿(function()
{
	//#region 初始化模块
	var __m = m();
	_helper.addInit(__m, init);

	$('head').append('<link rel="stylesheet" href="' + __m + 'S01/' + $('html').attr('lang') + '/css/common.css">');
    //#endregion

	//#region 初始化页面
	function init(instance, page, panel, url, reload)
	{
		if (reload)
		{
			if (page == 'course_list')
			{
				var course = new Course(instance, panel);
				ali.grid($('[data-ali-widget=grid]', panel), 'option', 'onMove', course.move);
			}
			else if (page == 'category_list')
			{
				var category = new Category(instance, panel);
				ali.grid($('[data-ali-widget=grid]', panel), 'option', 'onMove', category.move);
			}
			return;
		}

		if (page == 'config_basic')
		{
			//创建对象
			var instanceConfig = new InstanceConfig(instance, panel);

			//绑定事件
			$('form:first', panel).submit(instanceConfig.setBasicSettings);
		}
		else if (page == 'category_list')
		{
			//创建对象
			var category = new Category(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Display],[data-operation=UnDisplay],[data-operation=Recommend],[data-operation=UnRecommend]', category.update)
			     .on('click', '[data-operation=Delete]', category.del);
			ali.grid($('[data-ali-widget=grid]', panel), 'option', 'onMove', category.move);
		}
		else if (page == 'category_add')
		{
			//创建对象
			var category = new Category(instance, panel);

			//绑定事件
			$('form:first', panel).submit(category.add);
		}
		else if (page == 'category_edit')
		{
			//创建对象
			var category = new Category(instance, panel);

			//绑定事件
			$('form:first', panel).submit(category.modify);
		}
		else if (page == 'course_add')
	    {
	    	//创建对象
	    	var course = new Course(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(course.add);
	    	panel.on('click', '[data-operation=SelectPicture]', course.onSelectPictureClick);

	    }
	    else if (page == 'course_edit')
	    {
	    	//创建对象
	    	var course = new Course(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(course.modify);
	    	panel.on('click', '[data-operation=SelectPicture]', course.onSelectPictureClick);

			//初始化
			course.init(page);
	    }
	    else if (page == 'course_list')
	    {
			//创建对象
	    	var course = new Course(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Recycle],[data-operation=Recover],[data-operation=Delete],[data-operation=Release],[data-operation=UnRelease],[data-operation=Recommend],[data-operation=UnRecommend]', course.update)
				 .on('change', '[name=IsReleased]', course.onIsReleasedChange)
				 .on('click', '.select-all', course.onSelectAllClick)
				 .on('click', '.select-reverse', course.onSelectReverseClick);

			ali.grid($('[data-ali-widget=grid]', panel), 'option', 'onMove', course.move);
		}
	    else if (page == 'course_stat_order_all')
	    {
			//创建对象
	    	var course = new Course(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', course.onStatOrderAllFilterClick)
				 .on('click', '.stat1-list thead th', course.onStatListHeadClick);

	    	//初始化
			course.init(page);
		}
	    else if (page == 'course_stat_order_one')
	    {
			//创建对象
	    	var course = new Course(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', course.onStatOrderOneFilterClick);

	    	//初始化
			course.init(page);
		}
		else if (page == 'lesson_add')
	    {
	    	//创建对象
	    	var lesson = new Lesson(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(lesson.add);
	    	panel.on('click', '[name=IsChapter]', lesson.onIsChapterClick)
				.on('click', '[name=MediaType]', lesson.onTypeClick)
				.on('change', '[name=Url]', lesson.onUrlChange)
				 .on('click', '[data-operation=GetDuration]', lesson.onGetDurationClick);

			//初始化
			lesson.init(page);
	    }
	    else if (page == 'lesson_edit')
	    {
	    	//创建对象
	    	var lesson = new Lesson(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(lesson.modify);
			panel.on('click', '[name=MediaType]', lesson.onTypeClick)
				 .on('change', '[name=Url]', lesson.onUrlChange)
				 .on('click', '[data-operation=GetDuration]', lesson.onGetDurationClick);
	    }
	    else if (page == 'lesson_list')
	    {
			//创建对象
	    	var lesson = new Lesson(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Delete]', lesson.del);
		}
		else if (page == 'teacher_add')
	    {
	    	//创建对象
	    	var teacher = new Teacher(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(teacher.add);
	    	panel.on('click', '[data-operation=SelectPicture]', teacher.onSelectPictureClick)
				 .on('click', '[data-operation=SelectUser]', teacher.onSelectUserClick)
				 .on('click', '[data-operation=ClearUser]', teacher.onClearUserClick)
				 .on('click', '[data-page=user_select_dialog] td a', teacher.onUserClick);

	    }
	    else if (page == 'teacher_edit')
	    {
	    	//创建对象
	    	var teacher = new Teacher(instance, panel);

	    	//绑定事件
	    	$('form:first', panel).submit(teacher.modify);
	    	panel.on('click', '[data-operation=SelectPicture]', teacher.onSelectPictureClick)
				 .on('click', '[data-operation=SelectUser]', teacher.onSelectUserClick)
				 .on('click', '[data-operation=ClearUser]', teacher.onClearUserClick)
				 .on('click', '[data-page=user_select_dialog] td a', teacher.onUserClick);
		}
	    else if (page == 'teacher_stat_order_all')
	    {
			//创建对象
	    	var teacher = new Teacher(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', teacher.onStatOrderAllFilterClick)
				 .on('click', '.stat1-list thead th', teacher.onStatListHeadClick);

	    	//初始化
			teacher.init(page);
		}
	    else if (page == 'teacher_stat_order_one')
	    {
			//创建对象
	    	var teacher = new Teacher(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', teacher.onStatOrderOneFilterClick);

	    	//初始化
			teacher.init(page);
		}
	    else if (page == 'teacher_stat_money')
	    {
			//创建对象
	    	var teacher = new Teacher(instance, panel);

			//绑定事件
			panel.on('click', '[name=Income]', teacher.onStatMoneyFilterClick)
				 .on('click', '[data-operation=Settle]', teacher.onSettleClick)
				 .on('submit', '[data-page=settle] form', teacher.onSettleSubmit);

	    	//初始化
			teacher.init(page);
		}
	    else if (page == 'teacher_list')
	    {
			//创建对象
	    	var teacher = new Teacher(instance, panel);

			//绑定事件
	    	panel.on('click', '[data-operation=Recycle],[data-operation=Recover],[data-operation=Delete]', teacher.update);
		}
		else if (page == 'promotion_add')
		{
			//创建对象
            var promotion = new Promotion(instance, panel);

			//绑定事件
			$('form:first', panel).submit(promotion.add);
		}
        else if (page == 'promotion_edit')
		{
			//创建对象
            var promotion = new Promotion(instance, panel);

			//绑定事件
            $('form:first', panel).submit(promotion.modify);
		}
        else if (page == 'promotion_list')
		{
			//创建对象
            var promotion = new Promotion(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Recycle],[data-operation=Recover],[data-operation=Delete]', promotion.update)
		}
		else if (page == 'order_list')
		{
			//创建对象
			var order = new Order(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Finish]', order.onFinishClick)
				 .on('click', '[data-operation=Archive]', order.onArchiveClick)
				 .on('click', '[data-operation=Cancel]', order.onCancelClick)
				 .on('click', '[data-operation=Delete]', order.onDeleteClick)
				 .on('click', '[data-operation=Pay]', order.onPayClick)
				 .on('submit', '[data-page=order_pay] form', order.onPaySubmit)
				 .on('click', '[data-operation=Refund]', order.onRefundClick)
				 .on('submit', '[data-page=order_refund] form', order.onRefundSubmit)
				 .on('click', '[data-operation=Deliver]', order.onDeliverClick)
				 .on('submit', '[data-page=order_deliver] form', order.onDeliverSubmit)
				 .on('click', '[data-operation=Return]', order.onReturnClick)
			     .on('submit', '[data-page=order_return] form', order.onReturnSubmit);
		}
	    else if (page == 'order_stat')
	    {
			//创建对象
			var order = new Order(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', order.onStatFilterClick);

	    	//初始化
			order.init(page);
		}
		else if (page == 'member_level_add')
		{
			//创建对象
			var memberLevel = new MemberLevel(instance, panel);
			
			//绑定事件
			$('form:first', panel).submit(memberLevel.add);
			panel.on('click', '[name=CanApply]', memberLevel.onCanApplyClick);
		}
		else if (page == 'member_level_edit')
		{
			//创建对象
			var memberLevel = new MemberLevel(instance, panel);
			
			//绑定事件
			$('form:first', panel).submit(memberLevel.modify);
			panel.on('click', '[name=CanApply]', memberLevel.onCanApplyClick);

			//初始化
			memberLevel.init(page);
		}
		else if (page == 'member_level_list')
		{
			//创建对象
			var memberLevel = new MemberLevel(instance, panel);
			
			//绑定事件
			panel.on('click', '[data-operation=Delete]', memberLevel.del);
		}
		else if (page == 'member_order_list')
		{
			//创建对象
			var memberOrder = new MemberOrder(instance, panel);

			//绑定事件
			panel.on('click', '[data-operation=Finish]', memberOrder.onFinishClick)
				 .on('click', '[data-operation=Archive]', memberOrder.onArchiveClick)
				 .on('click', '[data-operation=Cancel]', memberOrder.onCancelClick)
				 .on('click', '[data-operation=Delete]', memberOrder.onDeleteClick)
				 .on('click', '[data-operation=Pay]', memberOrder.onPayClick)
				 .on('submit', '[data-page=member_order_pay] form', memberOrder.onPaySubmit)
				 .on('click', '[data-operation=Refund]', memberOrder.onRefundClick)
				 .on('submit', '[data-page=member_order_refund] form', memberOrder.onRefundSubmit)
				 .on('click', '[data-operation=Deliver]', memberOrder.onDeliverClick)
				 .on('submit', '[data-page=member_order_deliver] form', memberOrder.onDeliverSubmit)
				 .on('click', '[data-operation=Return]', memberOrder.onReturnClick)
			     .on('submit', '[data-page=member_order_return] form', memberOrder.onReturnSubmit);
		}
		/*
		else if (page == 'member_order_edit')
		{
			//创建对象
			var memberOrder = new MemberOrder(instance, panel);

			//绑定事件
			$('form:first', panel).submit(memberOrder.modify);
			panel.on('click', '[data-operation=SelectParent]', memberOrder.onSelectParentClick)
				 .on('click', '[data-operation=ClearParent]', memberOrder.onClearParentClick)
				 .on('click', '[data-page=parent_select_dialog] td a', memberOrder.onParentClick);
		}
		*/
		else if (page == 'member_stat')
	    {
			//创建对象
			var memberOrder = new MemberOrder(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', member.onStatFilterClick);

	    	//初始化
			memberOrder.init(page);
		}
	    else if (page == 'member_list')
	    {
			//创建对象
	    	var member = new Member(instance, panel);

			//绑定事件
	    	panel.on('click', '[data-operation=Recycle],[data-operation=Recover],[data-operation=Delete]', member.update);
		}
	    else if (page == 'member_stat_money')
	    {
			//创建对象
			var member = new Member(instance, panel);

			//绑定事件
			panel.on('click', '[name=Income]', member.onStatMoneyFilterClick)
				 .on('click', '[data-operation=Settle]', member.onSettleClick)
				 .on('submit', '[data-page=settle] form', member.onSettleSubmit);

	    	//初始化
			member.init(page);
		}
	    else if (page == 'member_stat_order_one')
	    {
			//创建对象
			var member = new Member(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', member.onStatOrderOneFilterClick);

	    	//初始化
			member.init(page);
		}
		else if (page == 'member_stat_order_all')
	    {
			//创建对象
			var member = new Member(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', member.onStatOrderAllFilterClick)
				 .on('click', '.stat1-list thead th', member.onStatListHeadClick);

	    	//初始化
			member.init(page);
		}
		else if (page == 'member_stat_member_all')
	    {
			//创建对象
			var member = new Member(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', member.onStatMemberAllFilterClick)
				 .on('click', '.stat1-list thead th', member.onStatListHeadClick);

	    	//初始化
			member.init(page);
		}
	    else if (page == 'member_stat_member_one')
	    {
			//创建对象
			var member = new Member(instance, panel);

			//绑定事件
			panel.on('click', '[name=IsAll],[name=IsFinished],[name=IsArchived],[name=IsCanceled],[name=IsPayed],[name=IsDelivered]', member.onStatMemberOneFilterClick);

	    	//初始化
			member.init(page);
		}
	}
	//#endregion

	//#region 栏目设置
	function InstanceConfig(instance, panel)
	{
		var __object = o(arguments.callee);

		this.setBasicSettings = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'SetBasicSettings', data: data });

			return false;
		};

		function prepareData(form)
		{
			if (!form.CourseThumbnailWidth.value.match(/^\d*$/))
			{
				ali.error(l(__m, 'course_thumbnail_width_incorrect'), $.proxy(function () { form.CourseThumbnailWidth.select(); }, this));
				return null;
			}

			if (!form.CourseThumbnailHeight.value.match(/^\d*$/))
			{
				ali.error(l(__m, 'course_thumbnail_height_incorrect'), $.proxy(function () { form.CourseThumbnailHeight.select(); }, this));
				return null;
			}

			var data = _helper.serialize(form);

			return data;
		}
	}
	//#endregion

	//#region 类别
	function Category(instance, panel)
	{
		var __object = o(arguments.callee);

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data });

			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			var id = v(panel, 'id');

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });

			return false;
		};

		this.del = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');
			ali.confirm(_lang_confirm_delete, $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					//服务器中删除
					_helper.post({ instance: instance, object: __object, operation: 'Delete', params: '&ID=' + id });

					//客户端中删除
					ali.grid($('[data-ali-widget=grid]', panel), 'deleteRow', tr[0].rowIndex);
				}

			}, this));

			return false;
		};

		this.update = function ()
		{
			_helper.update({ instance: instance, object: __object, element: this });
		};

		this.move = function (element, from, to)
		{
			_helper.post({ instance: instance, object: __object, operation: 'Move', params: 'From=' + from + '&To=' + to });
		};

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			var data = _helper.serialize(form);

			return data;
		}
	}
	//#endregion

	//#region 课程
	function Course(instance, panel)
	{
		var __object = o(arguments.callee);

		this.init = function (page)
		{
			if (page == 'course_edit')
			{
				var timeUnit = $('[name=TimeUnit]');
				timeUnit.val(v(panel, 'timeUnit'));
				ali.combo(timeUnit, 'refresh', timeUnit[0].selectedIndex);
			}
			else if (page == 'course_stat_order_one')
			{
				statOrderOne();
			}
			else if (page == 'course_stat_order_all')
			{
				statOrderAll();
			}
		}

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data });
			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			var id = v(panel, 'id');

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });
			return false;
		};

		this.onSelectAllClick = function ()
		{
			if (!$('[name=ListByPicture]', panel).prop('checked'))
				return;

			$('.course-list :checkbox', panel).prop('checked', true);
		};

		this.onSelectReverseClick = function ()
		{
			if (!$('[name=ListByPicture]', panel).prop('checked'))
				return;

			$('.course-list :checkbox', panel).each(function () { this.checked = !this.checked; });
		};

		this.update = function ()
		{
			if ($('[name=ListByPicture]', panel).prop('checked'))
			{
				var id = '';
				$('.course-list :checked', panel).each(function () { id += id == '' ? $(this).val() : '|' + $(this).val(); });
				_helper.update({ instance: instance, object: __object, id: id, element: this });
			}
			else
			{
				_helper.update({ instance: instance, object: __object, element: this });
			}
		};

		this.onSelectPictureClick = function ()
		{
			_helper.openSelectDialog({ element: this, instance: instance, object: __object, operation: 'UploadPicture', folder: 'course/' });
		};

		this.onIsReleasedChange = function ()
		{
			if (this.value == 'true')
			{
				$('.unrelease', panel).show();
				$('.release', panel).hide();
			}
			else if (this.value == 'false')
			{
				$('.release', panel).show();
				$('.unrelease', panel).hide();
			}
			else
			{
				$('.release', panel).hide();
				$('.unrelease', panel).hide();
			}
		};

		this.move = function (element, from, to)
		{
			_helper.post({ instance: instance, object: __object, operation: v(panel, 'move'), params: 'From=' + from + '&To=' + to });
		};

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			if (!_helper.isAllowedType(form.Picture.value, form.PictureAllowedType.value))
			{
				ali.error(l(__m, 'picture_type_must_be') + form.PictureAllowedType.value, $.proxy(function () { form.Picture.select(); }, this));
				return null;
			}

			//#region 限制大图大小，生成小图
			var thumbnailPath = form.Picture.value;
			if (form.Picture.value.indexOf('/') == 0)
			{
				var dot = form.Picture.value.lastIndexOf('.');
				thumbnailPath = form.Picture.value.substr(0, dot) + '_small' + form.Picture.value.substr(dot);
				$.post(_webPath + 'alicms.aspx?Mark=System&Object=Utility&Operation=ScaleImage&Source=' + encodeURIComponent(form.Picture.value) + '&Target=' + encodeURIComponent(thumbnailPath) + '&Width=' + v(panel, 'thumbanilWidth') + '&Height=' + v(panel, 'thumbanilHeight'), {}, function ()
				{
					$.post(_webPath + 'alicms.aspx?Mark=System&Object=Utility&Operation=ScaleImage&Source=' + encodeURIComponent(form.Picture.value) + '&Target=' + encodeURIComponent(form.Picture.value) + '&MaxWidth=600&MaxHeight=600', {});
				});
			}
			//#endregion

			var data = _helper.serialize(form);
			data += '&Thumbnail=' + encodeURIComponent(thumbnailPath);

			return data;
		}

		this.onStatOrderAllFilterClick = function()
		{
			statOrderAll();
		};

		function statOrderAll()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&Operation=Course_StatAll';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日统计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__todayStat = result;
				statSort(__todayStat, $('[data-name=Today] .sort', panel).data('col'), $('[data-name=Today] tbody', panel));
			});
	
			//本月统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__monthStat = result;
				statSort(__monthStat, $('[data-name=Month] .sort', panel).data('col'), $('[data-name=Month] tbody', panel));
			});
	
			//本年统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__yearStat = result;
				statSort(__yearStat, $('[data-name=Year] .sort', panel).data('col'), $('[data-name=Year] tbody', panel));
			});
		};

		this.onStatListHeadClick = function()
		{
			var index = $(this).parent().children().index(this);
			if (index < 2 || $(this).hasClass('sort'))
				return;

			$(this).siblings('.sort').removeClass('sort');
			$(this).addClass('sort');
				
			var container = $(this).closest('div');
			if (container.data('name') == 'Today')
				statSort(__todayStat, $('.sort', container).data('col'), $('tbody', container));
			else if (container.data('name') == 'Month')
				statSort(__monthStat, $('.sort', container).data('col'), $('tbody', container));
			else if (container.data('name') == 'Year')
				statSort(__yearStat, $('.sort', container).data('col'), $('tbody', container));
		}

		function statSort(list, col, target)
		{
			if (col == 'Count')
				list.sort(sortByCount);
			else if (col == 'Sum')
				list.sort(sortBySum);
			else if (col == 'AgentCommision')
				list.sort(sortByAgentCommision);
			else if (col == 'TeacherCommision')
				list.sort(sortByTeacherCommision);

			var html = '';
			for (var i in list)
			{
				html += '<tr><td>' + (parseInt(i) + 1) + '</td><th><a href="' + _helper.toAbsPanel(panel, 'edit.aspx') + '?ID=' + list[i].CourseInnerID + '" target="ali-navtab">' + list[i].CourseName + '</a></th><td>' + list[i].Count + '</td><td>' + list[i].Sum + '</td><td>' + list[i].AgentCommision + '</td><td>' + list[i].TeacherCommision + '</td></tr>';
			}
			target.html(html);
		}
		function sortByCount(a, b)
		{
			return b.Count - a.Count;
		}
		function sortBySum(a, b)
		{
			return b.Sum - a.Sum;
		}
		function sortByAgentCommision(a, b)
		{
			return b.AgentCommision - a.AgentCommision;
		}
		function sortByTeacherCommision(a, b)
		{
			return b.TeacherCommision - a.TeacherCommision;
		}

		this.onStatOrderOneFilterClick = function()
		{
			statOrderOne();
		};

		function statOrderOne()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&CourseID=' + v(panel, 'courseId');
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Course_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Course_StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Course_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Course_StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Course_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Course_StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Course_StatOne', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
		};
	}
	//#endregion

	//#region 课件
	function Lesson(instance, panel)
	{
		var __object = o(arguments.callee);

		this.init = function ()
		{
			var page = v(panel, 'page');

			if (page == 'lesson_add')
			{
				debugger;
				$.getJSON(_helper.toAbsPanel(panel, 'ajax_add.aspx?CourseID=' + $('[name=CourseID]', panel).val()), function (result)
				{
					$('[name=Sort]', panel).val(result.sort);
				});
			}
		};

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data });
			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			var id = v(panel, 'id');

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });
			return false;
		};

		this.del = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');
			ali.confirm(_lang_confirm_delete, $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					//服务器中删除
					_helper.post({ instance: instance, object: __object, operation: 'Delete', params: 'ID=' + id });

					//客户端中删除
					ali.grid($('[data-ali-widget=grid]', panel), 'deleteRow', tr[0].rowIndex);
				}

			}, this));

			return false;
		};

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			var data = _helper.serialize(form);

			return data;
		}

		this.onIsChapterClick = function ()
		{
			$('[data-for-chapter=false]', panel).toggle();
		};
		this.onTypeClick = function ()
		{
			$('[data-player=' + this.value + ']', panel).show().siblings().hide();
			$('[data-player=' + $('[name=MediaType]:checked', panel).val() + ']', panel).attr('src', $('[name=Url]', panel).val());
		};
		this.onUrlChange = function ()
		{
			$('[data-player=' + $('[name=MediaType]:checked', panel).val() + ']').attr('src', this.value);
		};
		this.onGetDurationClick = function ()
		{
			var player = $('[data-player=' + $('[name=MediaType]:checked', panel).val() + ']', panel)[0];
			var d = player.duration;
			$('[name=Duration]', panel).val(d ? parseInt(d) : 0);
		};
	}
	//#endregion

	//#region 教师
	function Teacher(instance, panel)
	{
		var __object = o(arguments.callee),
			__todayStat,
			__monthStat,
			__yearStat;

		this.init = function (page)
		{
			if (page == 'teacher_stat_order_one')
			{
				statOrderOne();
			}
			else if (page == 'teacher_stat_order_all')
			{
				statOrderAll();
			}
			else if (page == 'teacher_stat_money')
			{
				statMoney();

				$.get(_helper.toAbsPanel(panel, '../ajax_get_balance.aspx?CompanyID=' + _app.companyId + '&UserID=' + v(panel, 'userId')), function (result)
				{
					$('.stat1-balance em', panel).text($.trim(result));
				});	
			}
		};

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data });
			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			var id = v(panel, 'id');

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });
			return false;
		};

		this.update = function ()
		{
			_helper.update({ instance: instance, object: __object, element: this });
		};

		this.onSelectPictureClick = function ()
		{
			_helper.openSelectDialog({ element: this, instance: instance, object: __object, operation: 'UploadPicture', folder: 'course/' });
		};

		this.onSelectUserClick = function ()
		{
			ali.dialog({ name: 'SelectUser', modal: true, appendTo: panel, width: 400, height: 200, resizable: false, url: _helper.toAbsPanel(panel, '../user_select_dialog.aspx?Lang=' + _lang), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onUserClick = function ()
		{			
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			var userId = $(this).data('id');
			if ($('[name=UserID]', panel).val() == userId)
				return false;

			var userName = $(this).text();
			if ($('[name=UserID]', panel).data('old') == userId)
			{
				changeUser(userId, userName);
				return false;
			}

			$.getJSON(_helper.toAbsPanel(panel, 'ajax_user_id_exist.aspx?UserID=' + userId), function (result)
			{
				if ($.trim(result) == '0')
					changeUser(userId, userName);
				else
					ali.error(l(__m, 'user_id_exist'));
			});

			return false;
		};

		this.onClearUserClick = function ()
		{
			$('[name=UserName]', panel).val('');
			$('[name=UserID]', panel).val('');

			return false;
		};
		
		function changeUser(userId, userName)
		{
			$('[name=UserName]', panel).val(userName);
			$('[name=UserID]', panel).val(userId);
		}

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			if (!_helper.isAllowedType(form.Picture.value, form.PictureAllowedType.value))
			{
				ali.error(l(__m, 'picture_type_must_be') + form.PictureAllowedType.value, $.proxy(function () { form.Picture.select(); }, this));
				return null;
			}

			//#region 限制大图大小，生成小图
			var thumbnailPath = form.Picture.value;
			if (form.Picture.value.indexOf('/') == 0)
			{
				var dot = form.Picture.value.lastIndexOf('.');
				thumbnailPath = form.Picture.value.substr(0, dot) + '_small' + form.Picture.value.substr(dot);
				$.post(_webPath + 'alicms.aspx?Mark=System&Object=Utility&Operation=ScaleImage&Source=' + encodeURIComponent(form.Picture.value) + '&Target=' + encodeURIComponent(thumbnailPath) + '&Width=' + v(panel, 'thumbanilWidth') + '&Height=' + v(panel, 'thumbanilHeight'), {}, function ()
				{
					$.post(_webPath + 'alicms.aspx?Mark=System&Object=Utility&Operation=ScaleImage&Source=' + encodeURIComponent(form.Picture.value) + '&Target=' + encodeURIComponent(form.Picture.value) + '&MaxWidth=600&MaxHeight=600', {});
				});
			}
			//#endregion

			var data = _helper.serialize(form);
			data += '&Thumbnail=' + encodeURIComponent(thumbnailPath);

			return data;
		}

		this.onStatOrderAllFilterClick = function()
		{
			statOrderAll();
		};

		function statOrderAll()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&Operation=Teacher_StatAll';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日统计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__todayStat = result;
				statSort(__todayStat, $('[data-name=Today] .sort', panel).data('col'), $('[data-name=Today] tbody', panel));
			});
	
			//本月统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__monthStat = result;
				statSort(__monthStat, $('[data-name=Month] .sort', panel).data('col'), $('[data-name=Month] tbody', panel));
			});
	
			//本年统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__yearStat = result;
				statSort(__yearStat, $('[data-name=Year] .sort', panel).data('col'), $('[data-name=Year] tbody', panel));
			});
		};

		this.onStatListHeadClick = function()
		{
			var index = $(this).parent().children().index(this);
			if (index < 2 || $(this).hasClass('sort'))
				return;

			$(this).siblings('.sort').removeClass('sort');
			$(this).addClass('sort');
				
			var container = $(this).closest('div');
			if (container.data('name') == 'Today')
				statSort(__todayStat, $('.sort', container).data('col'), $('tbody', container));
			else if (container.data('name') == 'Month')
				statSort(__monthStat, $('.sort', container).data('col'), $('tbody', container));
			else if (container.data('name') == 'Year')
				statSort(__yearStat, $('.sort', container).data('col'), $('tbody', container));
		}

		function statSort(list, col, target)
		{
			if (col == 'Count')
				list.sort(sortByCount);
			else if (col == 'Sum')
				list.sort(sortBySum);
			else if (col == 'TeacherCommision')
				list.sort(sortByTeacherCommision);

			var html = '';
			for (var i in list)
			{
				html += '<tr><td>' + (parseInt(i) + 1) + '</td><th><a href="' + _helper.toAbsPanel(panel, 'edit.aspx') + '?ID=' + list[i].TeacherInnerID + '" target="ali-navtab">' + list[i].TeacherName + '</a></th><td>' + list[i].Count + '</td><td>' + list[i].Sum + '</td><td>' + list[i].TeacherCommision + '</td></tr>';
			}
			target.html(html);
		}
		function sortByCount(a, b)
		{
			return b.Count - a.Count;
		}
		function sortBySum(a, b)
		{
			return b.Sum - a.Sum;
		}
		function sortByTeacherCommision(a, b)
		{
			return b.Price - a.Price;
		}

		this.onStatOrderOneFilterClick = function()
		{
			statOrderOne();
		};

		function statOrderOne()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&TeacherID=' + v(panel, 'teacherId');
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Teacher_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.TeacherCommision);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Teacher_StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Teacher_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.TeacherCommision);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Teacher_StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Teacher_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.TeacherCommision);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Teacher_StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Teacher_StatOne', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.TeacherCommision);
			});
		};

		this.onStatMoneyFilterClick = function()
		{
			statMoney();
		};

		function statMoney()
		{
			alert('TODO');
			return;
			/*
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Company&Object=UserMoney&CompanyID=' + _app.companyId + '&UserID=' + v(panel, 'userId');
			path += '&Income=' + $('[name=Income]:checked', panel).val();
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=TodaySum]', panel).text(result);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Company_User_StatSumHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=MonthSum]', panel).text(result);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Company_User_StatSumDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=YearSum]', panel).text(result);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Company_User_StatSumMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.get(path + '&Operation=Company_User_StatSumOne', function (result)
			{
				$('[data-name=AllSum]', panel).text(result);
			});
			*/
		};

		this.onSettleClick = function ()
		{
			alert('TODO');
			return false;
			/*
			ali.dialog({ name: 'Settle', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, '../settle.aspx?CompanyID=' + _app.companyId + '&UserID=' + v(panel, 'userId')), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
			*/
		};

		this.onSettleSubmit = function ()
		{
			alert('TODO');
			return false;
			/*
			var data = _helper.serialize(this);
			data += '&Type=ALICOURSE_SETTLE_FOR_TEACHER';

			//发送
			_helper.post({ mark: 'Company', object: 'UserWithdrawal', operation: 'Settle', data: data, params: '' });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			ali.navTab('.ali-navtab', 'reload');

			return false;
			*/
		};
	}
	//#endregion

	//#region 促销
	function Promotion(instance, panel)
	{
		var __object = o(arguments.callee);

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;
			
			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data });

			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			var id = v(panel, 'id');
			
			//提交
            _helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });

			return false;
		};

		this.update = function ()
		{
			_helper.update({ instance: instance, object: __object, element: this });
		};

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			var data = _helper.serialize(form);

			//结束时间
			var end = new Date(form.StartTime.value);
			end.setDate(end.getDate() + parseInt(form.Duration.value));
			data += '&EndTime=' + ali.util.formatDate(end, 'yyyy-MM-dd');
			
			var course = $(':selected', form.CourseID);
			data += '&CourseName=' + encodeURIComponent(course.data('name'));
			data += '&CourseThumbnail=' + encodeURIComponent(course.data('picture'));

			return data;
		}
	}
	//#endregion

	//#region 订单
	function Order(instance, panel)
	{
		var __object = o(arguments.callee);

		this.init = function (page)
		{
			if (page == 'order_stat')
			{
				stat();
			}
		};

		this.onFinishClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_finish'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Finish', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onArchiveClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_archive'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Archive', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onCancelClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_cancel'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Cancel', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onDeleteClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_delete'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Delete', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onPayClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Pay', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'pay.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onPaySubmit = function ()
		{
			if (this.PaymentID.selectedIndex == -1)
			{
				ali.error(l(__m, 'payment_empty'), $.proxy(function () { this.PaymentID.select(); }, this));
				return false;
			}

			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.PayTime.value))
			{
				ali.error(l(__m, 'pay_time_incorrect'), $.proxy(function () { this.PayTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);
			
			//支付方式
			var payment = $(':selected', this.PaymentID);
			data += '&PaymentName=' + encodeURIComponent(payment.text());
			data += '&PaymentType=' + encodeURIComponent(payment.data('type'));
			data += '&PaymentAccountName=' + encodeURIComponent(payment.data('accountName'));
			data += '&PaymentAccountNo=' + encodeURIComponent(payment.data('account'));

			//发送
			_helper.post({ instance: instance, object: __object, operation: 'Pay', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onRefundClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Refund', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'refund.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onRefundSubmit = function ()
		{
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.RefundTime.value))
			{
				ali.error(l(__m, 'refund_time_incorrect'), $.proxy(function () { this.PayTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);

			_helper.post({ instance: instance, object: __object, operation: 'Refund', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};
		
		this.onDeliverClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Deliver', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'deliver.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onDeliverSubmit = function ()
		{
			//检查发货时间
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.DeliverTime.value))
			{
				ali.error(l(__m, 'deliver_time_incorrect'), $.proxy(function () { this.DeliverTime.select(); }, this));
				return false;
			}

			//准备
			var data = _helper.serialize(this);

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Deliver', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onReturnClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Return', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'return.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onReturnSubmit = function ()
		{
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.ReturnTime.value))
			{
				ali.error(l(__m, 'return_time_incorrect'), $.proxy(function () { this.ReturnTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);

			_helper.post({ instance: instance, object: __object, operation: 'Return', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onStatFilterClick = function()
		{
			stat();
		};

		function stat()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Stat', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
		};
	}
	//#endregion

	//#region 会员等级
	function MemberLevel(instance, panel)
	{
		var __object = o(arguments.callee);

		this.init = function (page)
		{
			if (page == 'member_level_edit')
			{
				var timeUnit = $('[name=TimeUnit]');
				timeUnit.val(v(panel, 'timeUnit'));
				ali.combo(timeUnit, 'refresh', timeUnit[0].selectedIndex);
			}
		}

		this.add = function ()
		{
			var data = prepareData(this);
			if (!data)
				return false;

			_helper.post({ instance: instance, object: __object, operation: 'Add', data: data});

			return false;
		};

		this.modify = function ()
		{
			var data = prepareData(this);
			if (data == null)
				return false;

			var id = v(panel, 'id');
			_helper.post({ instance: instance, object: __object, operation: 'Modify', data: data, params: 'ID=' + id });

			return false;
		};

		this.del = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(_lang_confirm_delete, $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					//服务器中删除
					_helper.post({ instance: instance, object: __object, operation: 'Delete', params: 'ID=' + id });

					//客户端中删除
					ali.grid($('[data-ali-widget=grid]', panel), 'deleteRow', tr[0].rowIndex);
				}

			}, this));

			return false;
		};
		
		this.onCanApplyClick = function()
		{
			if (this.value == 'true')
				$(this).closest('tr').next().show();
			else
				$(this).closest('tr').next().hide();
		};

		function prepareData(form)
		{
			if (form.Name.value == '')
			{
				ali.error(l(__m, 'name_empty'), $.proxy(function () { form.Name.select(); }, this));
				return null;
			}

			if (!form.Commision.value.match(/^\d+(\.\d{1,2})?$/))
			{
				ali.error(l(__m, 'commision_incorrect'), $.proxy(function () { form.Commision.select(); }, this));
				return null;
			}

			var data = _helper.serialize(form);

			return data;
		}
	}
	//#endregion

	//#region 会员订单
	function MemberOrder(instance, panel)
	{
		var __object = o(arguments.callee);

		this.init = function (page)
		{
			if (page == 'member_order_stat')
			{
				stat();
			}
		};

		this.onFinishClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_finish'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Finish', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onArchiveClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_archive'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Archive', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onCancelClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_cancel'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Cancel', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onDeleteClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.confirm(l(__m, 'confirm_delete'), $.proxy(function (returnValue)
			{
				if (returnValue)
				{
					_helper.post({ instance: instance, object: __object, operation: 'Delete', params: 'ID=' + id});

					//刷新页面
					$('.search1', panel).submit();
				}
			}, this));
		};

		this.onPayClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Pay', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'pay.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onPaySubmit = function ()
		{
			if (this.PaymentID.selectedIndex == -1)
			{
				ali.error(l(__m, 'payment_empty'), $.proxy(function () { this.PaymentID.select(); }, this));
				return false;
			}

			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.PayTime.value))
			{
				ali.error(l(__m, 'pay_time_incorrect'), $.proxy(function () { this.PayTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);
			
			//支付方式
			var payment = $(':selected', this.PaymentID);
			data += '&PaymentName=' + encodeURIComponent(payment.text());
			data += '&PaymentType=' + encodeURIComponent(payment.data('type'));
			data += '&PaymentAccountName=' + encodeURIComponent(payment.data('accountName'));
			data += '&PaymentAccountNo=' + encodeURIComponent(payment.data('account'));

			//发送
			_helper.post({ instance: instance, object: __object, operation: 'Pay', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onRefundClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Refund', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'refund.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onRefundSubmit = function ()
		{
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.RefundTime.value))
			{
				ali.error(l(__m, 'refund_time_incorrect'), $.proxy(function () { this.PayTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);

			_helper.post({ instance: instance, object: __object, operation: 'Refund', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};
		
		this.onDeliverClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Deliver', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'deliver.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onDeliverSubmit = function ()
		{
			//检查发货时间
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.DeliverTime.value))
			{
				ali.error(l(__m, 'deliver_time_incorrect'), $.proxy(function () { this.DeliverTime.select(); }, this));
				return false;
			}

			//准备
			var data = _helper.serialize(this);

			//提交
			_helper.post({ instance: instance, object: __object, operation: 'Deliver', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onReturnClick = function ()
		{
			var tr = $(this).closest('tr');
			var id = tr.data('id');

			ali.dialog({ name: 'Return', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, 'return.aspx?ID=' + id), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			return false;
		};

		this.onReturnSubmit = function ()
		{
			var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
			if (!dateTime.test(this.ReturnTime.value))
			{
				ali.error(l(__m, 'return_time_incorrect'), $.proxy(function () { this.ReturnTime.select(); }, this));
				return false;
			}

			var data = _helper.serialize(this);

			_helper.post({ instance: instance, object: __object, operation: 'Return', data: data, params: 'ID=' + $(this).data('id') });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			$('.search1', panel).submit();

			return false;
		};

		this.onStatFilterClick = function()
		{
			stat();
		};

		function stat()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=MemberOrder';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Stat&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(3)', tr).text(result.TeacherCommision);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td><td>' + result[i].AgentCommision + '</td><td>' + result[i].TeacherCommision + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Stat', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
				$('td:eq(2)', tr).text(result.AgentCommision);
				$('td:eq(5)', tr).text(result.TeacherCommision);
			});
		};
	}
	//#endregion

	//#region 会员
	function Member(instance, panel)
	{
		var __object = o(arguments.callee),
			__todayStat,
			__monthStat,
			__yearStat;

		this.init = function (page)
		{
			if (page == 'member_stat_order_one')
			{
				statOrderOne();
			}
			else if (page == 'member_stat_order_all')
			{
				statOrderAll();
			}
			else if (page == 'member_stat_member_all')
			{
				statMemberAll();
			}
			else if (page == 'member_stat_member_one')
			{
				statMemberOne();
			}
			else if (page == 'member_stat_money')
			{
				statMoney();

				$.get(_helper.toAbsPanel(panel, '../ajax_get_balance.aspx?UserID=' + v(panel, 'userId')), function (result)
				{
					$('.stat1-balance em', panel).text($.trim(result));
				});	
			}
		};

		this.update = function ()
		{
			_helper.update({ instance: instance, object: __object, element: this });
		};

		this.onStatOrderAllFilterClick = function()
		{
			statOrderAll();
		};

		function statOrderAll()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&Operation=Agent_StatAll';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
		
			//今日统计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__todayStat = result;
				statSort(__todayStat, $('[data-name=Today] .sort', panel).data('col'), $('[data-name=Today] tbody', panel));
			});
	
			//本月统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__monthStat = result;
				statSort(__monthStat, $('[data-name=Month] .sort', panel).data('col'), $('[data-name=Month] tbody', panel));
			});
	
			//本年统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__yearStat = result;
				statSort(__yearStat, $('[data-name=Year] .sort', panel).data('col'), $('[data-name=Year] tbody', panel));
			});
		};

		this.onStatListHeadClick = function()
		{
			var index = $(this).parent().children().index(this);
			if (index < 2 || $(this).hasClass('sort'))
				return;

			$(this).siblings('.sort').removeClass('sort');
			$(this).addClass('sort');
				
			var container = $(this).closest('div');
			if (container.data('name') == 'Today')
			{
				if (v(panel, 'page') == 'member_stat_member_all')
					statMemberSort(__todayStat, $('.sort', container).data('col'), $('tbody', container));
				else
					statSort(__todayStat, $('.sort', container).data('col'), $('tbody', container));
			}
			else if (container.data('name') == 'Month')
			{
				if (v(panel, 'page') == 'member_stat_member_all')
					statMemberSort(__monthStat, $('.sort', container).data('col'), $('tbody', container));
				else
					statSort(__monthStat, $('.sort', container).data('col'), $('tbody', container));
			}
			else if (container.data('name') == 'Year')
			{
				if (v(panel, 'page') == 'member_stat_member_all')
					statMemberSort(__yearStat, $('.sort', container).data('col'), $('tbody', container));
				else
					statSort(__yearStat, $('.sort', container).data('col'), $('tbody', container));
			}
		}

		function statSort(list, col, target)
		{
			if (col == 'Count')
				list.sort(sortByCount);
			else if (col == 'Sum')
				list.sort(sortBySum);

			var html = '';
			for (var i in list)
			{
				html += '<tr><td>' + (parseInt(i) + 1) + '</td><th><img src="' + list[i].AgentHead + '" style="height:30px" />' + list[i].AgentNickname + '</th><td>' + list[i].Count + '</td><td>' + list[i].Sum + '</td></tr>';
			}
			target.html(html);
		}
		function sortByCount(a, b)
		{
			return b.Count - a.Count;
		}
		function sortBySum(a, b)
		{
			return b.Sum - a.Sum;
		}

		this.onStatOrderOneFilterClick = function()
		{
			statOrderOne();
		};

		function statOrderOne()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=Order&AgentID=' + v(panel, 'agentId');
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
		
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Agent_StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Agent_StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Agent_StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Agent_StatOne', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
		};

		this.onStatMemberAllFilterClick = function()
		{
			statMemberAll();
		};

		function statMemberAll()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=MemberOrder&Operation=Agent_StatAll';
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
		
			//今日统计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__todayStat = result;
				statMemberSort(__todayStat, $('[data-name=Today] .sort', panel).data('col'), $('[data-name=Today] tbody', panel));
			});
	
			//本月统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__monthStat = result;
				statMemberSort(__monthStat, $('[data-name=Month] .sort', panel).data('col'), $('[data-name=Month] tbody', panel));
			});
	
			//本年统计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Start=' + start + '&End=' + end, function (result)
			{
				__yearStat = result;
				statMemberSort(__yearStat, $('[data-name=Year] .sort', panel).data('col'), $('[data-name=Year] tbody', panel));
			});
		};

		function statMemberSort(list, col, target)
		{
			if (col == 'Count')
				list.sort(sortByCount);
			else if (col == 'Sum')
				list.sort(sortBySum);

			var html = '';
			for (var i in list)
			{
				html += '<tr><td>' + (parseInt(i) + 1) + '</td><th><img src="' + list[i].AgentHead + '" style="height:30px" />' + list[i].AgentNickname + '</th><td>' + list[i].Count + '</td><td>' + list[i].Sum + '</td></tr>';
			}
			target.html(html);
		}

		this.onStatMemberOneFilterClick = function()
		{
			statMemberOne();
		};

		function statMemberOne()
		{
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Course&Object=MemberOrder&AgentID=' + v(panel, 'agentId');
			if (!$('[name=IsAll]', panel).prop('checked'))
			{
				path += '&IsFinished=' + $('[name=IsFinished]', panel).prop('checked');
				path += '&IsArchived=' + $('[name=IsArchived]', panel).prop('checked');
				path += '&IsCanceled=' + $('[name=IsCanceled]', panel).prop('checked');
			}

			var isPayed = $('[name=IsPayed]:checked', panel).val();
			if (isPayed != '')
				path += '&IsPayed=' + isPayed;
			
			var isDelivered = $('[name=IsDelivered]:checked', panel).val();
			if (isDelivered != '')
				path += '&IsDelivered=' + isDelivered;
		
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=TodaySum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Agent_StatHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=MonthSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Agent_StatDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.getJSON(path + '&Operation=Agent_StatOne&Start=' + start + '&End=' + end, function (result)
			{
				var tr = $('[data-name=YearSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Agent_StatMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Count + '</td><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.getJSON(path + '&Operation=Agent_StatOne', function (result)
			{
				var tr = $('[data-name=AllSum]', panel);
				$('td:eq(0)', tr).text(result.Count);
				$('td:eq(1)', tr).text(result.Sum);
			});
		};

		this.onStatMoneyFilterClick = function()
		{
			statMoney();
		};

		function statMoney()
		{
			alert('//TODO');
			/*
			var now = new Date();

			var path = '/alicms.aspx?Lang=' + _lang + '&Mark=Company&Object=UserMoney&CompanyID=' + _app.companyId + '&UserID=' + v(panel, 'userId');
			path += '&Income=' + $('[name=Income]:checked', panel).val();
	
			//今日合计
			var start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 00:00:00'));
			var end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=TodaySum]', panel).text(result);
			});
	
			//今日统计
			$.getJSON(path + '&Operation=Company_User_StatSumHourly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Hour + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Today] tbody', panel).html(html);
			});
	
			//本月合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=MonthSum]', panel).text(result);
			});
	
			//本月统计
			$.getJSON(path + '&Operation=Company_User_StatSumDaily&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Day.substr(8) + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Month] tbody', panel).html(html);
			});
	
			//本年合计
			start = encodeURIComponent(ali.util.formatDate(now, 'yyyy-01-01 00:00:00'));
			end = encodeURIComponent(ali.util.formatDate(now, 'yyyy-MM-dd 23:59:59'));
			$.get(path + '&Operation=Company_User_StatSumOne&Start=' + start + '&End=' + end, function (result)
			{
				$('[data-name=YearSum]', panel).text(result);
			});
	
			//本年统计
			$.getJSON(path + '&Operation=Company_User_StatSumMonthly&Start=' + start + '&End=' + end, function (result)
			{
				var html = '';
				for (var i in result)
				{
					html += '<tr><th>' + result[i].Month.substr(5) + '</th><td>' + result[i].Sum + '</td></tr>';
				}
				$('[data-name=Year] tbody', panel).html(html);
			});
	
			//全部订单
			$.get(path + '&Operation=Company_User_StatSumOne', function (result)
			{
				$('[data-name=AllSum]', panel).text(result);
			});
			*/
		};

		this.onSettleClick = function ()
		{
			alert('//TODO');
			/*
			ali.dialog({ name: 'Settle', modal: true, appendTo: panel, width: 600, resizable: false, url: _helper.toAbsPanel(panel, '../settle.aspx?CompanyID=' + _app.companyId + '&UserID=' + v(panel, 'userId')), onBeforeLoad: _helper.onDialogBeforeLoad, onShow: _helper.onDialogShow });
			*/
			return false;
		};

		this.onSettleSubmit = function ()
		{
			alert('//TODO');
			/*
			var data = _helper.serialize(this);
			data += '&Type=ALICOURSE_SETTLE_FOR_TEACHER';

			//发送
			_helper.post({ mark: 'Company', object: 'UserWithdrawal', operation: 'Settle', data: data, params: '' });

			//关闭对话框
			ali.dialog($(this).closest('.ali-dialog-body').children(), 'close');

			//刷新页面
			ali.navTab('.ali-navtab', 'reload');

			*/
			return false;
		};
	}
	//#endregion
})();
//@ sourceURL=AliCMS.CourseModule.js































